'use strict';
const Service = require('egg').Service;
const puppeteer = require('puppeteer');

class Pdf extends Service{
	async create(data){
        const ctx = this.ctx;

        // 启动Chromium
        const browser = await puppeteer.launch({ignoreHTTPSErrors: true, headless:false, args: ['--no-sandbox']});
        // 打开新页面
        const page = await browser.newPage();
        // 设置页面分辨率
        await page.setViewport({width: 1600, height: 900});

        // let request_url = 'http://192.168.0.93:8080/workflow/solex/yf/iddzzz/index.html#/designer';
        let request_url = data.url;

        // 访问
        await page.goto(request_url, {waitUntil: 'domcontentloaded'}).catch(err => console.log(err));
        await page.waitFor(3000);

        try {
            let fileName = ctx.helper.generateSecret(10);
            // 截图
            await page.screenshot({path: './app/public/images/'+ data.date + fileName +'.jpg', fullPage:true})
            return ctx.helper.success({url:'/public/images/'+ data.date + fileName +'.jpg'},"截图成功");

        } catch (e) {
            return ctx.helper.fail('异常');
        } finally {
            await browser.close();
        }
	}
}
module.exports = Pdf;